{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ast\n",
    "import json\n",
    "import os\n",
    "from datasets import load_dataset\n",
    "\n",
    "ds = load_dataset(\"codeparrot/apps\", split=\"train\", trust_remote_code=True)\n",
    "print(ds)\n",
    "print(ds[0]['difficulty'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# APPS dataset has difficulties as strings: introductory, interview, competition\n",
    "# Let introductory->2.5, interview->5.5, competition->8.5\n",
    "def difficulty_to_int(difficulty):\n",
    "    if difficulty == \"introductory\":\n",
    "        return 2.5\n",
    "    if difficulty == \"interview\":\n",
    "        return 5.5\n",
    "    if difficulty == \"competition\":\n",
    "        return 8.5\n",
    "    return -1\n",
    "\n",
    "def process_dataset(ds):\n",
    "    dataset = []\n",
    "    for entry in ds:\n",
    "        tests = entry[\"input_output\"]\n",
    "        if not tests:\n",
    "            continue\n",
    "        if isinstance(tests, str):\n",
    "            try:\n",
    "                tests = ast.literal_eval(entry[\"input_output\"])\n",
    "            except (ValueError, SyntaxError) as e:\n",
    "                # Try Json loads instead\n",
    "                try:\n",
    "                    tests = json.loads(entry[\"input_output\"])\n",
    "                except (json.JSONDecodeError, SyntaxError, ValueError) as e:\n",
    "                    print(repr(entry[\"input_output\"]))\n",
    "                    print(f\"Error in json.loads: {e}\")\n",
    "                    continue\n",
    "        assert isinstance(tests, dict), \"Tests should be a dictionary\"\n",
    "        # Mkae it a proper json dict\n",
    "        if len(tests[\"inputs\"]) <= 1:\n",
    "            continue\n",
    "        new_entry = {\n",
    "            \"problem\": entry[\"question\"],\n",
    "            \"tests\": tests,\n",
    "        }\n",
    "        dataset.append(new_entry)\n",
    "    return dataset\n",
    "\n",
    "train_dataset = process_dataset(ds)\n",
    "print(len(train_dataset))\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"apps.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(train_dataset, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load json file\n",
    "with open(output_file, \"r\") as f:\n",
    "    train_dataset = json.load(f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm-xiao",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
